<?php
//author:liuchenguang
//功能：私信管理
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Session;
use think\Db;
use app\index\model\Message as Msg;

class Message extends Controller
{
   	public function displayside()
    {
    	//$user_id= Session::get("user_id");
    	$user_id=3;
    	$sql = "select distinct tb_user.user_name,tb_user.user_id from tb_user,tb_msg where tb_msg.receiver_id =".$user_id." and tb_msg.topic =2  and tb_msg.sender_id = tb_user.user_id";
		$list = Db::query($sql);
    	$this->assign('side_list',$list);
    	return  $this->fetch('message');
	}
	
	public function getmesgbytopic(Request $request){
    	//$user_id = Session::get("user_id");
    	$user_id=3;
		$list = Msg::all(["topic"=>$request->param("topic"),"receiver_id"=>$user_id]);
		return $list;
	}
	
	public function getmesgbyuserid(Request $request){
		$sender_id = $request->param("sender_id");
//		$receiver_id = Session::get("user_id");
		$receiver_id=3;
		$list=Db::table('tb_msg')->where("sender_id",'in',[$sender_id,$receiver_id])
		->where("receiver_id",'in',[$sender_id,$receiver_id])->order('time','desc')->select();
		return $list;
	}
	
	public function addtalkmsg(Request $request){
		//$sender_id = Session::get("user_id");
		$sender_id=3;
		
		$Msg = new Msg;
		$Msg->topic=2;
		$Msg->sender_id=$sender_id;
		$Msg->receiver_id=$request->param("receiver_id");
		$Msg->time=date("Y-m-d H:i:s");
		$Msg->content=$request->param("content");
		$Msg->is_read=0;
		if($Msg->save()){
			return json("0");
		}
		else{
			return json("1");
		}
	}
}
?>